package com.nosnow.edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nosnow.common.utils.R;
import com.nosnow.edu.entity.EduTeacher;
import com.nosnow.edu.entity.vo.TeacherQuery;
import com.nosnow.edu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 讲师管理
 *
 * @author bingheng
 * @since 2020-03-29
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * @RequestBody 使用json传递数据，把json数据封装到对应的对象里面去
     * 1. 使用   @RequestBody 需要使用POST方式提交
     * 2. required = false  表示这个值可以没有（多条件搜索的条件都可以为空的）
     * */
    @ApiOperation(value="分页条件查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 1. 创建page对象
        Page<EduTeacher> page = new Page<>(current, limit);

        // 2. 构建多条件组合查询, 判断条件值是否为空，如果不为空则动态拼接（原理：mybatis 动态sql）
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        // 2.1 如果讲师名字不为空，则拼接讲师名字的模糊查询条件
        if(!StringUtils.isEmpty(teacherQuery.getName())){
            wrapper.like("name", teacherQuery.getName());
        }

        // 2.2 如果讲师的头衔不等于于空，则拼接上讲师头衔的查询条件
        if(!StringUtils.isEmpty(teacherQuery.getLevel())){
            wrapper.eq("level", teacherQuery.getLevel());
        }

        // 2.3 如果讲师开始时间不为空，则拼接讲师的时间查询条件 >= 开始时间
        if(!StringUtils.isEmpty(teacherQuery.getBegin())){
            wrapper.ge("gmt_create", teacherQuery.getBegin());
        }

        // 2.4 如果讲师的结束时间不为空，则拼接讲师的结束时间查询 <= 结束时间
        if(!StringUtils.isEmpty(teacherQuery.getEnd())) {
            wrapper.le("gmt_create", teacherQuery.getEnd());
        }

        // 2.5 添加时间排序值, 降序排序
        wrapper.orderByDesc("gmt_create");

        // 3. 调用方法实现条件分页查询, mp底层封装，把分页的所有数据封装到 page 对象里面
        eduTeacherService.page(page, wrapper);

        // 4. 返回结果给前端
        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    @ApiOperation(value="根据主键逻辑删除一位讲师")
    @DeleteMapping("remove/{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean flog = eduTeacherService.removeById(id);
        if(flog) {
            return  R.ok().message("删除讲师成功");
        } else{
            return R.error().message("删除讲师失败");
        }
    }

    @ApiOperation(value="添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flog = eduTeacherService.save(eduTeacher);
        if(flog) {
            return R.ok().message("添加讲师成功");
        } else{
            return R.error().message("添加讲师失败");
        }
    }

    @ApiOperation(value="根据Id查询讲师")
    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@ApiParam(name = "id", value = "讲师Id") @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    @ApiOperation(value="根据Id修改讲师信息")
    @PostMapping("updateTeacherById")
    public R updateTeacherById(@RequestBody EduTeacher eduTeacher) {
        boolean flog = eduTeacherService.updateById(eduTeacher);
        if(flog){
            return R.ok().message("修改讲师成功");
        } else {
            return R.error().message("修改讲师失败");
        }
    }
}

